/*
 * @lc app=leetcode.cn id=2 lang=golang
 *
 * [2] 两数相加
 */

// @lc code=start
/**
 * Definition for singly-linked list.
 * type ListNode struct {
 *     Val int
 *     Next *ListNode
 * }
 */

package main

func addTwoNumbers(l1 *ListNode, l2 *ListNode) *ListNode {
	return add(l1, l2, 0)
}

func add(l1 *ListNode, l2 *ListNode, tot int) *ListNode {
	if l1 == nil && l2 == nil && tot == 0 {
		return nil
	}
	if l1 != nil {
		tot += l1.Val
		l1 = l1.Next
	}
	if l2 != nil {
		tot += l2.Val
		l2 = l2.Next
	}
	return &ListNode{tot % 10, add(l1, l2, tot/10)}
}

// @lc code=end
